//
//  WBEmoticonKeyboardView.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/25.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit

class WBEmoticonKeyboardView: UIView {

    // MARK: -  重写init方法
    override init(frame: CGRect) {
        
        
        super.init(frame: frame)
        setUpUI()
    }
    
    // MARK: -  设置UI
    private func setUpUI() {
        
        backgroundColor = randumColor()
        
        addSubview(emoticonToolBar)
        addSubview(pictureCollectionView)
        addSubview(pageControl)
        
        emoticonToolBar.snp_makeConstraints { (make) -> Void in
            
            make.height.equalTo(37)
            make.leading.trailing.bottom.equalTo(self)
            
        }
        
       
        //给底部toolBar的闭包赋值
        emoticonToolBar.emoticonToolBarButtonClickClosure = { (type: WBEmoticonToolBarType) in
            
            switch type {
                
            case .defaultButton:
                print("默认")
                break
            case .emojiButton:
                print("emoji")
                break
            case .flowerButton:
                print("浪小花")
                break
                
            }
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            self.pictureCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            self.pageControl.numberOfPages = WBEmoticonTool.sharedTools.allEmoticons[indexPath.section].count
            self.pageControl.currentPage = 0
            
        }

        pictureCollectionView.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emoticonToolBar.snp_top)
        }
        pictureCollectionView.delegate = self
        
        pageControl.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(pictureCollectionView)
            make.centerX.equalTo(self)
        }
    }

    // MARK: -  重写init方法时必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  懒加载pageControl
    private lazy var pageControl: UIPageControl = {
        
        let pageControl = UIPageControl()
        pageControl.numberOfPages = WBEmoticonTool.sharedTools.allEmoticons[0].count
        pageControl.currentPage = 0
        //通过KVC给私有属性设置值
        
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")

        return pageControl
    }()
    
    // MARK: -  懒加载显示图片的collectionView WBEmoticonCollectionView
    private lazy var pictureCollectionView: WBEmoticonCollectionView = WBEmoticonCollectionView()
    
    // MARK: -  懒加载控件
    private lazy var emoticonToolBar: WBEmoticonToolBar = WBEmoticonToolBar()
}




// MARK: -  collectionView的代理方法
extension WBEmoticonKeyboardView: UICollectionViewDelegate {
    
    // MARK: -  当scrollView开始滚动时
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let centerX = scrollView.contentOffset.x + kScreenW / 2
        
        let cells = pictureCollectionView.visibleCells().sort { (cell1, cell2) -> Bool in
            
                return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        if cells.count == 2 {
            
            var indexPath: NSIndexPath?
            
            let firstCell = cells.first!
            let lastCell = cells.last!
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                
                indexPath = pictureCollectionView.indexPathForCell(firstCell)!
                
            }else {
                
                indexPath = pictureCollectionView.indexPathForCell(lastCell)!
            }
            
            //scrollView滚动时button也要跟着切换
            emoticonToolBar.setEmoticonCurrentButton(indexPath!.section)
            
            //scrollView滚动时 pageControl跟着改变
            pageControl.numberOfPages = WBEmoticonTool.sharedTools.allEmoticons[indexPath!.section].count
            pageControl.currentPage = indexPath!.item
            
        }
    }
}